package cn.com.dhc.danlu.widget.popup;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.math.BigDecimal;
import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.util.CommonConst;
import cn.com.dhc.danlu.util.CommonUtils;
import cn.com.dhc.danlu.util.StringUtil;
import cn.com.dhc.danlu.widget.DLToolbar;
import cn.com.dhc.danlu.widget.base.BasePopupWindow;
import cn.com.hd.mall.web.webservices.entity.response.order.OrderDetailResponse.OrderDetailBean.PromotionListBean;

/**
 * author: 李一方(<a href="mailto:liyifang@danlu.com">liyifang@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2017-03-10 11:35
 *
 * <p>
 * 促销信息的汇总的弹窗
 * </p>
 */
public class PromotionDetailPopup extends BasePopupWindow {
    private TextView tvGift;
    private TextView tvPrice;
    private LinearLayout llGift;
    private LinearLayout llPrice;
    private LinearLayout llGiftArea;
    private LinearLayout llPriceArea;

    private Context context;

    public PromotionDetailPopup(Context context, String discount, List<PromotionListBean> promotionList) {
        super(context);

        initSelf(context);
        initViews(context);
        filledContent(discount, promotionList);
    }

    private void initSelf(Context context) {
        this.context = context;

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setAnimationStyle(R.style.popwin_anim_style);
        setOutsideTouchable(true);
        setFocusable(true);
    }

    private void initViews(Context context) {
        View popView = LayoutInflater.from(context).inflate(R.layout.popup_shopcar_promotion_detail, (ViewGroup) ((Activity) context).getWindow().getDecorView(), false);
        setContentView(popView);
        initChildView(popView);
    }

    private void initChildView(View popView) {
        tvGift = (TextView) popView.findViewById(R.id.tv_gift);
        tvPrice = (TextView) popView.findViewById(R.id.tv_price);
        llGift = (LinearLayout) popView.findViewById(R.id.ll_gift);
        llPrice = (LinearLayout) popView.findViewById(R.id.ll_price);
        llGiftArea = (LinearLayout) popView.findViewById(R.id.ll_gift_area);
        llPriceArea = (LinearLayout) popView.findViewById(R.id.ll_price_area);

        DLToolbar dlToolbar = (DLToolbar) popView.findViewById(R.id.dl_toolbar);
        dlToolbar.setRightClickListenter(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }


    @SuppressLint("SetTextI18n")
    private void filledContent(String discount, List<PromotionListBean> promotionList) {
        if (TextUtils.isEmpty(discount) || "0".equals(discount)) {
            llPrice.setVisibility(View.GONE);
            llPriceArea.setVisibility(View.GONE);
        } else {
            llPrice.setVisibility(View.VISIBLE);
            llPriceArea.setVisibility(View.VISIBLE);
            tvPrice.setText("促销优惠: 减 ￥" + CommonUtils.fenToYuan(new BigDecimal(discount)).toPlainString() + "元");
        }

        if (hasGift(promotionList)) {
            llGift.setVisibility(View.VISIBLE);
            llGiftArea.setVisibility(View.VISIBLE);
            tvGift.setText("促销赠送：".concat(getGiftMsg(promotionList)));
        } else {
            llGift.setVisibility(View.GONE);
            llGiftArea.setVisibility(View.GONE);
        }

        for (PromotionListBean promotion : promotionList) {
            if (CommonConst.PromotionConst.PROMOTION_CHANGE_TYPE_0.equals(promotion.getPromotionType())) {
                llGiftArea.addView(getItemView(promotion.getMerchName(), promotion.getPromotionDetail(), promotion.getPromotionType()));
            } else {
                llPriceArea.addView(getItemView(promotion.getMerchName(), promotion.getPromotionDetail(), promotion.getPromotionType()));
            }
        }
    }

    private boolean hasGift(List<PromotionListBean> promotionList) {
        for (PromotionListBean promotion : promotionList) {
            if (CommonConst.PromotionConst.PROMOTION_CHANGE_TYPE_0.equals(promotion.getPromotionType())) {
                return true;
            }
        }
        return false;
    }

    private String getGiftMsg(List<PromotionListBean> promotionList) {
        String giftMsg = "";
        for (PromotionListBean promotion : promotionList) {
            if (CommonConst.PromotionConst.PROMOTION_CHANGE_TYPE_0.equals(promotion.getPromotionType())) {
                giftMsg = giftMsg.concat("送").concat(promotion.getPromotionDetail()).concat(";");
            }
        }
        return giftMsg;
    }

    private LinearLayout getItemView(String merchName, String promotionInfo, String type) {
        LinearLayout llItem = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.item_shopcar_promotion_detail, null, false);
        TextView tvType = (TextView) llItem.findViewById(R.id.tv_type);
        TextView tvDetail = (TextView) llItem.findViewById(R.id.tv_detail);
        switch (type) {
            case CommonConst.PromotionConst.PROMOTION_CHANGE_TYPE_0:
                // 满赠
                tvType.setText("【满赠】");
                tvDetail.setText(merchName + "  (" + promotionInfo + ")");
                break;

            case CommonConst.PromotionConst.PROMOTION_CHANGE_TYPE_1:
                // 满减
                tvType.setText("【满减】");
                tvDetail.setText(merchName + "  (" + promotionInfo + ")");
                break;

            case CommonConst.PromotionConst.PROMOTION_CHANGE_TYPE_4:
                // 限购
                tvType.setText("【限促】");
                tvDetail.setText(merchName + "  (" + promotionInfo + ")");
                break;

            default:
                break;
        }
        tvDetail.setText(StringUtil.hightLight(merchName + "  (" + promotionInfo + ")", ContextCompat.getColor(context, R.color.color_b12525), (int) tvDetail.getTextSize()));
        return llItem;
    }
}
